package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Picture;
import com.itheima.pojo.Result;
import com.itheima.pojo.StatusCode;
import com.itheima.service.PictureService;
import com.itheima.pojo.PictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/picture")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    /**
     *添加图片
     * @param pictureVOList
     */
    @RequestMapping("/add")
    public Result add(@RequestBody List<PictureVO> pictureVOList) {

        try {
            for (PictureVO pictureVO : pictureVOList) {
                pictureService.add(pictureVO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"插入失败");
        }

        //封装返回对象
        return new Result(StatusCode.OK,"插入成功");
    }

    /**
     * 删除图片
     * @param pictureVO
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody PictureVO pictureVO) {

        try {
            pictureService.delete(pictureVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"删除失败");
        }
        return new Result(StatusCode.OK,"删除成功");
    }

    /**
     * 编辑图片
     * @param pictureVO
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody PictureVO pictureVO) {

        try {
            pictureService.edit(pictureVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"更新失败");
        }
        return new Result(StatusCode.OK,"更新成功");
    }

    /**
     * 获取图片列表
     * @param pictureVO
     * @return
     */
    @RequestMapping("/getList")
    public Map<String,Object> getList(@RequestBody PictureVO pictureVO){

        PageInfo<Picture> pageInfo = pictureService.getList(pictureVO);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();

        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",null);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将data放入返回map
        result.put("code","success");
        result.put("data",data);
        //返回结果
        return result;
    }

    /**
     * 通过图片Uid将图片设为封面
     * @param pictureVO
     */
    @RequestMapping("/setCover")
    public Result setCover(@RequestBody PictureVO pictureVO) {

        try {
            pictureService.setCover(pictureVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"设置失败");
        }
        return new Result(StatusCode.OK,"设置成功");
    }
}
